package cn.imvc.app.models;

import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.security.access.ConfigAttribute;
import org.springframework.security.web.util.matcher.RequestMatcher;
import org.springframework.stereotype.Service;

import cn.imvc.app.entities.Goods;
import cn.imvc.app.entities.GoodsAttrValue;
import cn.imvc.app.entities.GoodsSpec;
import cn.imvc.app.repositories.GoodsSpecRepository;
import cn.imvc.core.ModelUtil;
import cn.imvc.data.domain.ViewRequest;

@Service
public class GoodsSpecModelImpl implements GoodsSpecModel {

	@Autowired
	GoodsSpecRepository goodsSpecRepository;
	@Autowired
	GoodsModel goodsModel;
	@Autowired
	GoodsSpecValueModel goodsSpecValueModel;
	@Autowired
	GoodsSkuModel goodsSkuModel;
	
	public LinkedHashMap<RequestMatcher, Collection<ConfigAttribute>> getRequestMap() {
		// TODO Auto-generated method stub
		return null;
	}

	public Page<GoodsSpec> find(ViewRequest viewRequest) {
		return goodsSpecRepository.findAll(ModelUtil.OrderByIdDesc(viewRequest));
	}

	public GoodsSpec get(int id) {
		return goodsSpecRepository.findOne(id);
	}

	/**
	 * 保存规格，如果已经存在sku，则商品下架重新审核，sku封存
	 */
	public GoodsSpec save(GoodsSpec goodsSpec) {
		Goods goods = goodsModel.get(goodsSpec.getGoods().getId());
		if(!goodsSpecRepository.findByGoodsAndSpecClass(goods, goodsSpec.getSpecClass()).isEmpty()){
			return null;
		}
		
		//v0.1如果存在sku了，则封存该商品sku，并且商品下架重新审核
		//v0.2如果存在sku了，则商品下架重新审核
		if(goodsSkuModel.getSkuBySpec("",goodsSpec.getGoods().getId()) > 0){
			goodsModel.updateAfterSpecChange(goodsSpec.getGoods().getId());
		}
		
		goodsSpec.setGoods(goods);
		return goodsSpecRepository.saveAndFlush(goodsSpec);
	}

	public GoodsSpec update(GoodsSpec goodsSpec) {
		GoodsSpec goodsSpecTmp = get(goodsSpec.getId());
		//v0.1如果存在sku了，则封存该商品sku，并且商品下架重新审核
		//v0.2如果存在sku了，则商品下架重新审核
		if(goodsSpecTmp.getSpecClass().getId() != goodsSpec.getSpecClass().getId()){
			if(goodsSkuModel.getSkuBySpec("",goodsSpecTmp.getGoods().getId()) > 0){
				goodsModel.updateAfterSpecChange(goodsSpec.getGoods().getId());
			}
		}
		
		Goods goods = goodsModel.get(goodsSpec.getGoods().getId());
		goodsSpec.setGoods(goods);
		return goodsSpecRepository.saveAndFlush(goodsSpec);
	}

	public String delete(int id) {
		GoodsSpec goodsSpec = get(id);
		int i = goodsSkuModel.getSkuBySpec(id+"", goodsSpec.getGoods().getId());
		if(i>0){
			return "该规格已被sku引用，不能删除";
		}else{
			goodsSpecValueModel.deleteByGoodsSpec(get(id));
			goodsSpecRepository.delete(id);
			return "";
		}
	}

	public String deleteBatch(int[] ids) {
		for (int id : ids) {
			String str = delete(id);
			if(!str.equals("")){
				return str;
			}
		}
		return "";
	}

	public List<GoodsSpec> findByGoods(Goods goods) {
		return goodsSpecRepository.findByGoods(goods);
	}

}
